package com.example.springbootServiceNetwork.demos.web.Config;

import com.example.springbootServiceNetwork.demos.web.Tool.TokenInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ConcurrentTaskExecutor;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.servlet.config.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;

@Configuration
@CrossOrigin
public class WebMvcConfig implements WebMvcConfigurer {
    /**
     * 开启跨域
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 设置允许跨域的路由
        registry.addMapping("/**")
                // 设置允许跨域请求的域名
                //.allowedOrigins("*")
                .allowedOriginPatterns("*")
                // 是否允许携带cookie参数
                .allowCredentials(true)
                // 设置允许的方法
                .allowedMethods("*")
                // 跨域允许时间
                .maxAge(3600);

    }

    @Resource
    private TokenInterceptor tokenInterceptor;

    //构造方法
    public WebMvcConfig(TokenInterceptor tokenInterceptor) {
        this.tokenInterceptor = tokenInterceptor;
    }
    @Override
    public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
        configurer.setTaskExecutor(new ConcurrentTaskExecutor(Executors.newFixedThreadPool(3)));
        configurer.setDefaultTimeout(30000);

    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/");

        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

        registry.addResourceHandler("/static/**")
                .addResourceLocations("file:/www/wwwroot/testdemo/static/");
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> excludePath = new ArrayList<>();
        //排除拦截，除了注册登录(此时还没token)，其他都拦截
        excludePath.add("/user/register"); //注册
        excludePath.add("/user/login");//登录
        excludePath.add("/user/getQRcode");//获取登录二维码信息
        excludePath.add("/user/sendEmail");//发送qq验证码
        excludePath.add("/user/emailLogin");//qq邮箱登录
        excludePath.add("/user/getCode");//获取图形验证码
        excludePath.add("/user/verifyCode");//校验图形验证码
        excludePath.add("/websocket/**");//websoct
        excludePath.add("/api/queryMenuItem");//查询菜单
        excludePath.add("/api/queryItem");//查询站点所有数据
        excludePath.add("/api/queryCate");//查询站点分类
        excludePath.add("/file/upload");//文件上传
        excludePath.add("/file/delete");//文件删除
        excludePath.add("/file/download");//文件下载
        excludePath.add("/file/downloadApk");//下载apk文件
        excludePath.add("/dict/queryDict");//查询字典
        excludePath.add("/alipay/pay");//支付宝支付
        excludePath.add("/alipay/sandPay");//二维码支付
        excludePath.add("/alipay/notify");//支付回调
        excludePath.add("/wx/backLogin");//微信登陆回调
        excludePath.add("/static/**");//资源访问
        excludePath.add("/tool/**");//工具类接口放行
        excludePath.add("/user/queryContributors");//查询贡献者
        registry.addInterceptor(tokenInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(excludePath);
        WebMvcConfigurer.super.addInterceptors(registry);

    }


}